package edu.fjs.liyq.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Pager implements Serializable{
	private long rowCount;              // 总的数据条数
	private int rowPerPage;             // 每页显示的数据条数
	private int currentPage;            // 当前页的页码
	private int pageCount;              // 总的页数
	private int pagesPerDisplay;        // 页码控件显示的页面按钮的个数
	private int displayCount;           // 
	private Integer[] displayPages;

	public Pager(long rowCount, int currentPage) {
		this(rowCount, 5, currentPage, 5);
	}

	public Pager(long rowCount, int rowPerPage, int currentPage) {
		this(rowCount, rowPerPage, currentPage, 5);
	}

	public Pager(long rowCount, int rowPerPage, int currentPage,
			int pagesPerDisplay) {
		super();
		this.rowCount = rowCount;
		this.rowPerPage = rowPerPage;
		this.currentPage = currentPage;
		this.pagesPerDisplay = pagesPerDisplay;
		this.pageCount = (int) Math.ceil((float) rowCount / rowPerPage);
		this.displayCount = (int) Math.ceil((float) this.pageCount
				/ pagesPerDisplay);
		initDisplayPages();
	}

	public long getRowCount() {
		return rowCount;
	}

	public int getRowPerPage() {
		return rowPerPage;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public int getFirstRow() {
		return (this.currentPage - 1) * this.rowPerPage;
	}

	public boolean isHavePrevPage() {
		return Math.ceil((float) this.currentPage / pagesPerDisplay) > 1;
	}

	public boolean isHaveNextPage() {
		return Math.ceil((float) this.currentPage / pagesPerDisplay) < displayCount;
	}

	public int getNextDisplayFirstPage() {
		return displayPages[displayPages.length - 1].intValue() + 1;
	}

	public int getPrevDisplayLastPage() {
		return displayPages[0].intValue() - 1;
	}

	public int getPageCount() {
            int pageCount = (int) Math.ceil((float) rowCount / rowPerPage);
		return pageCount;
	}

	public Integer[] getDisplayPages() {
		return this.displayPages;
	}

	public void initDisplayPages() {
		int currentDisplayNumber = (int) Math.ceil((float) this.currentPage
				/ pagesPerDisplay);
		List<Integer> pages = new ArrayList<Integer>();
		for (int i = ((currentDisplayNumber - 1) * pagesPerDisplay + 1); i <= currentDisplayNumber
				* pagesPerDisplay; i++) {
			if (((i - 1) * this.rowPerPage + 1) <= this.getRowCount())
				pages.add(i);
		}
		this.displayPages = (Integer[]) pages.toArray(new Integer[0]);
	}

    /**
     * @param rowCount the rowCount to set
     */
    public void setRowCount(long rowCount) {
        this.rowCount = rowCount;
    }
    
    public static void main(String[] args) {
        Pager pager = new Pager(0, 5, 1);
        pager.setRowCount(9);
        System.out.println(pager.getPageCount());
    }
}
